<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class UserView_list extends AppView
{
	
	var $template = 'list.html';
	var $template_popup = 'list_popup.html';
	
	
	function execute(&$obj, &$manager) {
		
		$this->addMsg('user_msg.ini');
		
		$tmpl = ($this->controller->getMoreParam('popup')) ? $this->template_popup :  $this->template;
		$tpl = &new tplTemplatez($this->template_dir . $tmpl);
		
		// filter 
		$manager->setSqlParams($this->getFilterSql($manager));		
		
		// header generate
		$bp =& $this->pageByPage($manager->limit, $manager->getRecordsSql());
		$tpl->tplAssign('header', $this->commonHeaderList($bp->nav, $this->getFilter($manager)));
		
		// sort generate
		$sort = &$this->getSort();
		$manager->setSqlParamsOrder($sort->getSql());
		
		// get records
		$users =& $this->stripVars($manager->getRecords($bp->limit, $bp->offset));
		
		// get user priv
		foreach($users as $row) { $ids[] = $row['id']; }
		$ids = implode(',', $ids);
		$user_priv =& $manager->getPrivByIds($ids);
		$user_priv = $this->stripVars($user_priv);
		
		//priv lang
		$msg = AppMsg::getMsgs('privileges_msg.ini');
		
		
		// list records
		foreach($users as $row) {
			
			$obj->set($row);
			$obj->setFullName();
			
			
			// user priv			
			$privilege = array();
			foreach($user_priv[$obj->get('id')] as $user_id => $v) {
				$privilege[] = $msg[$v]['name'];
			}
			$tpl->tplAssign('privilege', implode(', ',  $privilege));

			
			$tpl->tplAssign('name', $obj->getFullName());
			$tpl->tplAssign($this->getViewListVars($obj->get('id'), $obj->get('active')));
			
			$tpl->tplParse(array_merge($obj->get(), $this->msg), 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($sort->toHtml());
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function &getSort() {
		//$sort = new TwoWaySort();
		$sort = new OneWaySort($_GET);
		$sort->default_order = 2;
		$sort->setTitleMsg('asc',  $this->msg['sort_asc_msg']);
		$sort->setTitleMsg('desc', $this->msg['sort_desc_msg']);
		$sort->setSortItem('name_msg',     'last_name', 'last_name', $this->msg['name_msg']);
		$sort->setSortItem('email_msg',    'email',     'email',     $this->msg['email_msg']);
		$sort->setSortItem('phone_msg',    'phone',     'phone',     $this->msg['phone_msg']);
		$sort->setSortItem('username_msg', 'username',  'username',  $this->msg['username_msg']);
		$sort->setSortItem('status_msg',   'active',    'active',    $this->msg['status_msg']);
		//$sort->setSortItem('',             'sort',      'sort_order', '', 1);		
		
		//$sort->getSql();
		//$sort->toHtml()
		return $sort;
	}
	
	
	function getFilter($manager) {

		$select = new FormSelect();
		$select->css_class = 'colorInput';
		$select->setSelectWidth(250);
		$select->setOnChangeSubmit(true);
		$select->setSelectName('filter');
		$select->setRange($manager->getPrivSelectRange(), 
		                  array('all'=>$this->msg['all_priv_msg']));
		
		$arr = $_GET; unset($arr[$select->select_name]);		

		
		$html = '<form action="" style="margin: 0px;">';
		$html .= http_build_hidden($arr);
		$html .= $select->select();
		$html .= '</form>';
		
		return $html;
	}
	
	
	function getFilterSql($manager) {
		
		// filter
		$arr = array();
		if(isset($_GET['filter']) && $_GET['filter'] != 'all') {
			
			$id = (int) $_GET['filter'];
			$arr[] = "AND p.priv_name_id = '$id'";
		}
		
		return implode(" \n", $arr);
	}		
}
?>
